// var url = "http://42.96.185.45:8081/chuangwu/web/index.php?r=";
// var url = "http://115.28.48.128/chuangwu/web/index.php?r=";
var url = "http://localhost/chuangwu/web/index.php?r=";
// var canName = false;
var canPswd = false;
var canMobile = false;

function validMobile(){
	
	$.post("http://42.96.185.45:8081/chuangwu/user/validMobile",
		{mobile:$("#mobile").val()}, 
		function(obj){
			// alert(obj);
			// var jobj = jQuery.parseJSON('{"name": "John" }');
			// alert("name:"+jobj.name);
			// var obj2 = jQuery.parseJSON(obj); //解析出现问题
			// alert(json.code);
			// alert(obj.code);
			//写正则

			// $phonenumber = '13712345678';  
			// if(preg_match("/^1[34578]{1}\d{9}$/",$phonenumber)){  
			//     echo "是手机号码";  
			// }else{  
			//     echo "不是手机号码";  
			// }  
			// alert(obj.code);
			if(obj.code == 0){
				$("#mobileText").text("手机号可以使用");
				$("#mobileText").css('color', "gray");
				canMobile = true;
			}else{
				$("#mobileText").text("此手机号已经存在");
				$("#mobileText").css('color', "red");
				canMobile = false;
			}
	}, "json");

}

function validPasswd(){
	if($("#repasswd").val() != $("#password").val()){
		$("#repasswdText").text("两次密码不一致");
		$("#repasswdText").css('color', "red");
		canPswd = false;
	}else{
		$("#repasswdText").text("密码一致");
		$("#repasswdText").css('color', "gray");
		canPswd = true;
	}
}


function register(){
	//canPswd || canMobile || $("#checkedBox".checked)
	if(true){
		// alert(1);

		var username = $("#username").val();
		var password = $("#password").val(); 
		var email = $("#email").val();
		var mobile = $('#mobile').val();
		$.post("http://42.96.185.45:8081/chuangwu/user/register",
			{username:username,
			password:password,
			email:email,
			mobile:mobile,
			}, 	
			function(obj){
				if(obj.code == 0){
					var user = obj.data;
					document.cookie = "user_id=" + user.user_id;
					window.location.href = url+"index/show";
				}else{
					alert(obj.msg);
				}
			}, "json");
	}
}


function login(){
	$.post("http://42.96.185.45:8081/chuangwu/user/login",
	{
		password:$("#password").val(),
		mobile:$('#mobile').val(),
	}, function(obj){
		// alert(obj.msg); // 
		if(obj.code == 0){
			alert("登录成功");
			setTimeout(3000);
			var user = obj.data;
			// document.cookie = "user_id=" + user.user_id + ";path=/";

			var Days = 30;
			var exp = new Date();
			exp.setTime(exp.getTime() + Days*24*60*60*1000);
			document.cookie = "user_id" + "="+ user.user_id;

			location.href = url+"index/show"; //跳转到首页

		}else{
			alert("用户名或密码错误");
		}
	}, "json");
}


function logout(){
	alert("您已经退出登录....正在跳转到首页");
	setCookie("user_id", "", -1);
	location.href = "index.php?r=index/show"; //跳转到首页
}

function setCookie(cname, cvalue, exdays) {
	var d = new Date();
	d.setTime(d.getTime() + (exdays*24*60*60*1000));
	var expires = "expires="+d.toUTCString();
	document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCities(){

	var selectObj = $(".getProvince")[0];
    var optionObj = selectObj.options[selectObj.selectedIndex];
    var father_id = optionObj.id;

	$.get("index.php?r=usercenter/getprovince",{father_id:father_id}, function(data){
        var realData = JSON.parse(data);
        var count = realData.length;
		$(".getCities").empty();
        for(var i=0;i<count;i++){
			$(".getCities").append("<option>" + realData[i]['province'] + "</option>>");
		}

	});
}


function clearAll(user_id){
	$.get("http://42.96.185.45:8081/chuangwu/msg/clearAll", {user_id:user_id}, function(data){ //妈的直接调java接口好了
        if(data.code == 1){
            alert(data.msg);
        }else{
            location.href = url+"mymsg/show";
        }
	});
}

function readAll(user_id){
	$.get("http://42.96.185.45:8081/chuangwu/msg/readAll", {user_id:user_id}, function(data){
        if(data.code == 1){
            alert(data.msg);
        }else{
            location.href = url+"mymsg/show";
        }
	});
}


function update(user_id){
    var user_name = $("#a12").text();
    var sex = $("#a22").text();
	var sexNumber;
	sexNumber = sex == "男"?1:2;
    var identityCard = $("#a32").text();

    // var password = $("#a32").text();
    var mobile = $("#a79").text();
	var email = $("#a84").text();
	var field = document.getElementsByClassName("field")[0].value;
	var selfIntro = document.getElementById("selfIntro").value;
	var province = document.getElementsByClassName("getProvince")[0].value;
	var wechat = document.getElementById("a42").value;
	var school = document.getElementById("a52").value;
	// alert(school);
	var city = document.getElementsByClassName("getCities")[0].value;
	city = city == "--请选择--"?"":city;
    var detailAddr = document.getElementById("detailAddr").value;

	$.post("http://42.96.185.45:8081/chuangwu/user/updateUser_v1", {
		userid:user_id,
		user_name:user_name,
		sex:sexNumber,
		identityCard:identityCard,
		mobile:mobile,
		email:email,
		field:field,

		selfIntro:selfIntro,
		wechat:wechat,
		school:school,
		address:province+city+detailAddr
	},function(data){
		if(data.code == 0){
			alert("修改成功");
			location.href = url+"usercenter/show";

		}else{
			alert("修改失败 请与管理员联系");
		}
	});


function jsCopy(){
	var clip = new ZeroClipboard( document.getElementById("copy"), {
		moviePath: "ZeroClipboard.swf"
	} );

// 复制内容到剪贴板成功后的操作
	clip.on( 'complete', function(client, args) {
		alert("复制成功，复制内容为："+ args.text);
	} );
}


}


